package cn.fldong.employee.dao;


import cn.fldong.employee.entity.EmployeeEntity;

import java.sql.ResultSet;
import java.util.ArrayList;

public class EmployeeDao {
    // 定义一个方法，根据员工ID查询员工信息
    public EmployeeEntity queryByEmpID(String empID ) {
        DBUtil dao=new DBUtil();
        String sql="select * FROM Employees WHERE empID=? ";
        ResultSet resultSet=dao.executeQuery(sql,new String[]{ empID });
        try {
            if(resultSet.next()){
                //empName, empGender, empAge, empDepartment, empPosition, empHireDate, empSalary
                EmployeeEntity employee=new EmployeeEntity();
                employee.setEmpID(resultSet.getInt("empID"));
                employee.setEmpName(resultSet.getString("empName"));
                employee.setEmpGender(resultSet.getString("empGender"));
                employee.setEmpAge(resultSet.getInt("empAge"));
                employee.setEmpDepartment(resultSet.getString("empDepartment"));
                employee.setEmpPosition(resultSet.getString("empPosition"));
                employee.setEmpHireDate(resultSet.getString("empHireDate"));
                employee.setEmpSalary(resultSet.getBigDecimal("empSalary"));
                return employee;
            }
        } catch (Exception e) {
                e.printStackTrace();
        } finally {
            dao.closeConnection();
        }
        return null ;
    }
    // 定义一个方法，查询所有员工信息
    public ArrayList<EmployeeEntity> queryAll() {
        DBUtil dao = new DBUtil();
        String sql = "select * FROM Employees";
        ResultSet resultSet = dao.executeQuery(sql, null);
        try {
            ArrayList<EmployeeEntity> employees = new ArrayList();
            while (resultSet.next()) {
                //empName, empGender, empAge, empDepartment, empPosition, empHireDate, empSalary
                EmployeeEntity employee = new EmployeeEntity();
                employee.setEmpID(resultSet.getInt("empID"));
                employee.setEmpName(resultSet.getString("empName"));
                employee.setEmpGender(resultSet.getString("empGender"));
                employee.setEmpAge(resultSet.getInt("empAge"));
                employee.setEmpDepartment(resultSet.getString("empDepartment"));
                employee.setEmpPosition(resultSet.getString("empPosition"));
                employee.setEmpHireDate(String.valueOf(resultSet.getDate("empHireDate")));
                employee.setEmpSalary(resultSet.getBigDecimal("empSalary"));
                employees.add(employee);
            }
            return employees;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dao.closeConnection();
        }
        return null;
    }

    // 定义一个方法，新增员工信息
    public boolean addEmployee(EmployeeEntity employee) {
        DBUtil dao = new DBUtil();
        String sql = "INSERT INTO Employees(empID, empName, empGender, empAge, empDepartment, empPosition, empHireDate, empSalary) VALUES(?,?,?,?,?,?,?,?)";
        String[] params = new String[]{String.valueOf(employee.getEmpID()), employee.getEmpName(), employee.getEmpGender(),String.valueOf(employee.getEmpAge()), employee.getEmpDepartment(), employee.getEmpPosition(), employee.getEmpHireDate(), String.valueOf(employee.getEmpSalary())};
        int result = dao.executeUpdate(sql, params);
        if (result > 0) {
            return true;
        }
        return false;
    }

    // 定义一个方法，更新员工信息
    public boolean updateEmployee(EmployeeEntity employee) {
        DBUtil dao = new DBUtil();
        String sql = "UPDATE Employees SET empName=?, empGender=?, empAge=?, empDepartment=?, empPosition=?, empHireDate=?, empSalary=? WHERE empID=?";
        String[] params = new String[]{employee.getEmpName(), employee.getEmpGender(), String.valueOf(employee.getEmpAge()),employee.getEmpDepartment(), employee.getEmpPosition(), employee.getEmpHireDate(), String.valueOf(employee.getEmpSalary()), String.valueOf(employee.getEmpID())};
        int result = dao.executeUpdate(sql, params);
        if (result > 0) {
            return true;
        }
        return false;
    }

    // 定义一个方法，删除员工信息
    public boolean deleteEmployee(String empID) {
        DBUtil dao = new DBUtil();
        String sql = "DELETE FROM Employees WHERE empID=?";
        String[] params = new String[]{empID};
        int result = dao.executeUpdate(sql, params);
        if (result > 0) {
            return true;
        }
        return false;
    }
}
